package pt.isel.pdm.ouryamba;

import winterwell.jtwitter.Twitter.Status;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * Represents the fragment that is used in the TimelineActivity and DetailActivity do display the status details.
 * */
public class DetailFragment extends Fragment implements OnClickListener
{
	private General app;
	private View view;
	private TextView id;
	private TextView author;
	private TextView date;
	private TextView text;
	private Button button;
	private static final String newLine = "\n";

	
//OnClickListener methods

	/**
	 * Delegates the action to take when the the button to share the status through email is clicked.</br>
	 * 
	 * Constructs the message which will be seen in the email content and send the email.
	 * 
	 * @see General#sendEmail(String, String, String)
	 * */
	@Override
	public void onClick(View arg0)
	{
		String message = newLine + newLine + getString(R.string.shareMsg)
				+ newLine + getString(R.string.id) + General.SPACE_STRING + id.getText()
				+ newLine + getString(R.string.date) + General.SPACE_STRING + date.getText()
				+ newLine + getString(R.string.author) + General.SPACE_STRING + author.getText()
				+ newLine + getString(R.string.text) + General.SPACE_STRING + text.getText()
				+ newLine;
		app.sendEmail(null, getString(R.string.shareSubject), message);
	}
	
		
//Own methods
	
	/**
	 * Update all the views of this fragment.</br>
	 * 
	 * Depending on the parameter, update the views with the clicked status 
	 * ({@code toRefreshing} = false) or the with word "refreshing" indicating the status is not
	 * available yet ({@code toRefreshing} = true).</br>
	 * 
	 * It fetches the details from the database according to the item that was select on the ListView.
	 * If there was none item selected, the details fetched are about the status with higher ID which
	 * corresponds to the first element on the ListView.
	 * If the database has none data, an Toast is displayed so user can know that.
	 * 
	 * @param toRefreshing Indicates whether the views are to be updated with a status or not.
	 * @see General#getStatusSelectedId()
	 * @see General#dbSelectHighestID()
	 * @see General#dbSelectByID(long)
	 * @see General#showToast(int)
	 * */
	private void fillAllTextViews(boolean toRefreshing)
	{
		String _id, _author, _date, _text;
		_id = _author = _date = _text = General.EMPTY_STRING;
		if(toRefreshing)
		{
			_id = _author = _date = _text = getString(R.string.refreshing);
		}
		else
		{
			Status status = null;
			if(app.getStatusSelectedId() == General.DEFAULT_STATUS_SELECTED_ID)
			{
				status = app.dbSelectHighestID();				
			}
			else
			{
				status = app.dbSelectByID(app.getStatusSelectedId());
			}
			if(status != null)
			{
				_id = String.valueOf(status.id);
				_date = status.createdAt.toString();
				_text = status.text;
				_author = status.user.screenName; 
			}
			else
			{
				app.showToast(R.string.databaseEmpty);
			}
		}
		id.setText(General.SPACE_STRING + _id);
		author.setText(General.SPACE_STRING + _author);
		date.setText(General.SPACE_STRING + _date);
		text.setText(General.SPACE_STRING + _text);
	}
	
	/**
	 * Before updating the views, store their references, if they were not stored yet.</br>
	 * 
	 * This method is only to ensure that the views are not unreferenced before we update them,
	 * which would probably thrown a {@code NullPointerException}.
	 * 
	 * @param toRefreshing Indicates whether the views are to be updated with a status or not.
	 * @see #referenceComponents()
	 * @see #fillAllTextViews(boolean)
	 * */
	public void updateViewElements(boolean toRefreshing)
	{
		if(id == null)
		{
			referenceComponents();
		}
		fillAllTextViews(toRefreshing);
	}
	
	/**
	 * Fetch the reference of all the views of this fragment.
	 * */
	private void referenceComponents()
	{
		id = (TextView) view.findViewById(R.id.id_value_tv);
		author = (TextView) view.findViewById(R.id.author_value_tv);
		date = (TextView) view.findViewById(R.id.date_value_tv);
		text = (TextView) view.findViewById(R.id.text_value_tv);
		button = (Button) view.findViewById(R.id.share_BT);
		button.setOnClickListener(this);
	}
	
	
//Fragment methods
	
	/**
	 * Inflates the fragment layout and store the reference for the General class.
	 * */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		view = inflater.inflate(R.layout.fragment_detail, container);
		app = (General) getActivity().getApplication();
		return view;
	}

	/**
	 * Once the activity is created, if the fragment is inside the DetailActivity, it updates the 
	 * views with a selected status.
	 * 
	 * @see #updateViewElements(boolean)
	 * */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		if(getActivity() instanceof DetailActivity)
		{
			updateViewElements(false);
		}
	}
	
}
